package alf.mapping.view.activity;

import alf.mapping.view.Env;
import alf.mapping.view.touch.TouchReactor;
import alf.mapping.view.touch.TouchReactorCallback;
import alf.mapping.view.touch.TouchReactorManager;
import alf.mapping.view.visuals.EditorEngine;
import alf.mapping.view.visuals.Sprite;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MapView extends View implements TouchReactorCallback {

	private EditorEngine editor;
	private long lastUpdate;
	private Rect puntoGordo,selector;
	private Context ctx;
	private int alphaPuntoGordo =0;
	private TouchReactor reactor;

	private static final int TOUCH_GHOST_DISAPEARANCE_DELAY = 200;
	private static final int TOUCH_GHOST_DISAPEARANCE_RATE = 85;
	private static final int DEFAULT_ALFA = 64;

	public MapView(Context ctx, AttributeSet set) {
		super(ctx, set);
		this.ctx = ctx;
		init();
	}

	public void setTouchArea(Rect puntoGordo) {
		this.puntoGordo = puntoGordo;
		if (puntoGordo!=null){
			alphaPuntoGordo = DEFAULT_ALFA;
		}
	}

	public void setSelector(Rect selector) {
		this.selector = selector;
	}

	public Rect getSelector() {
		return selector;
	}

	private void init() {
		lastUpdate = SystemClock.elapsedRealtime();
		this.editor = EditorEngine.getInstance();
		this.reactor = new TouchReactorManager(this.ctx,this);
		editor.setSize(new Point(this.getWidth(), getHeight()));
		Env.debug("MapView.init.1");

		editor.drawBackground(this, true);
	}

	@Override
	public void onSizeChanged(int i0, int i1, int i2, int i3) {
		super.onSizeChanged(i0, i1, i2, i3);
		editor.setSize(new Point(getWidth(), getHeight()));
		Env.toast(getContext(), "resize:" + i0 + "," + i1);
	}

	@Override 
	public void onDraw(Canvas canvas) {
		final long now = SystemClock.elapsedRealtime();
		editor.drawBackground(this, false);
		// links
		for (Sprite sp: editor.getSprites()) {
			sp.drawLinks(canvas, editor.getViewport(), editor.getZoom(), editor.getCurrentRoom());
		}
		// sprites
		for (Sprite sp: editor.getSprites()) {
			sp.update(now);
			sp.onDraw(canvas, editor.getViewport(), editor.getZoom());
		}
		// selected item
		if (editor.getSelected() != null) {
			drawSelectedRect(canvas);
		}
		// puntogordo
		if (alphaPuntoGordo > 5) {
			if (now - lastUpdate > TOUCH_GHOST_DISAPEARANCE_DELAY) {
				alphaPuntoGordo -= TOUCH_GHOST_DISAPEARANCE_RATE * (now - lastUpdate) / 1000;
				if (alphaPuntoGordo < 0) {
					alphaPuntoGordo = 0;
				}
				lastUpdate = now;
			}
			drawRect(canvas, puntoGordo, Color.BLUE, alphaPuntoGordo);
		}
		if ( selector!=null) {
			drawRect(canvas, selector, Color.GRAY, 64);
		}
		invalidate();
	}

	private void drawSelectedRect(Canvas canvas) {
		if (editor.getSelected() != null) {
			for (Sprite sp: editor.getSelected()) {
				Rect bounds =new Rect(sp.rect());
				int inset = -5;
				bounds.inset(inset, inset);
				int color = Color.GRAY;
				drawRect(canvas, bounds, color, 64);
			}
		}
	}

	private void drawRect(Canvas canvas, Rect rect, int color, int alpha) {
		ShapeDrawable sh = new ShapeDrawable();
		Rect bounds = editor.model2scr(rect);
		sh.setBounds(bounds);
		sh.setColorFilter(new LightingColorFilter(0, color));
		sh.setAlpha(alpha);
		editor.model2scr(bounds);
		sh.draw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return reactor.onTouchEvent(event);
	}


}
